package com.http.method;

import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;

import com.http.HttpUtil;
import com.http.callback.RequestCallBack;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mac_wk on 14-11-19.
 */
public abstract class Request<T> implements Comparable<Request<T>>,Serializable {

    public Map<String,String> params= new HashMap<String,String>();;
    public Map<String,String> head = new HashMap<String,String>();
    public int timeout=5000;
    public int retry=0;
    private int method = HttpUtil.GET;
    private String mUrl;
    protected RequestCallBack requestCallBack;
    public boolean cancled = false;
    public static final int SUCCESS=1;
    public static final int ERROR = 2;
    private  static Handler default_handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            RequestResult<?> requestResult  = (RequestResult<?>) msg.obj;
            RequestCallBack requestCallBack1 =  requestResult.requestCallBack;
            switch (msg.what)
            {
                case SUCCESS:

                    if (requestCallBack1!=null)
                        requestCallBack1.success(requestResult.obj);
                    break;
                case ERROR:
                    if (requestCallBack1!=null)
                        requestCallBack1.error(requestResult.status_code, (String) requestResult.obj,requestResult.exception);
                    break;
            }
        }
    };
    protected  Handler handler = default_handler;
    public  Request(String mUrl)
    {
        this.mUrl = mUrl;
    }
    public   Request(String mUrl,RequestCallBack requestCallBack)
    {
        this.mUrl = mUrl;
        this.requestCallBack = requestCallBack;
    }
    public Request(String mUrl,RequestCallBack requestCallBack,Handler handler1)
    {
        this.mUrl = mUrl;
        this.requestCallBack = requestCallBack;
        handler = handler1;
    }
    public String getmUrl() {
        return mUrl;
    }

    public void setmUrl(String mUrl) {
        this.mUrl = mUrl;
    }

    public int getMethod() {
        return method;
    }

    public void setMethod(int method) {
        this.method = method;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    public boolean isCancled() {
        return cancled;
    }
    public void setCancled(boolean cancled) {
        this.cancled = cancled;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    protected String parseHttpUrlConnection(HttpURLConnection httpURLConnection) throws IOException {


        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        String tmp_str = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
             inputStream = httpURLConnection.getInputStream();
             bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

             String tmp = null;
             while ((tmp=bufferedReader.readLine())!=null)
             {
                    stringBuilder.append(tmp);
             }
            tmp_str = stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            if (bufferedReader!=null)
                bufferedReader.close();
            if (inputStream!=null)
                inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            inputStream.close();
            bufferedReader.close();
        }
        return tmp_str;

    }
    public abstract void setRequestType(String type);
    public abstract String getRequestType();
    public abstract void executeRequest(HttpURLConnection httpURLConnection) throws IOException;

    protected static class RequestResult<T>{
        RequestCallBack requestCallBack;
        T obj;
        int status_code;
        Exception exception;
    }
}
